package com.ebt.m.data.entity;

import android.text.TextUtils;

import com.ebt.m.customer.util.PinyinUtil;
import com.ebt.m.data.bean.ResponseProductTag;
import com.ebt.m.data.middle.ProductDatainfo;
import com.ebt.m.data.rxModel.apibean.ApiProduct;
import com.ebt.m.utils.ArrayUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 产品
 *
 * @author Michael.li
 */
public class ProductInfo implements Serializable, Comparable<ProductInfo> {

    /**
     *
     */
    private static final long serialVersionUID = -6413074392891159581L;

    public ProductInfo() {
    }

    public long b_time;
    public long b_count;
    /**
     * 主键
     */
    public int Id;
    /**
     * 产品表主键
     */
    public int ProductId;
    /**
     * 公司ID
     */
    public int CompanyId;
    /**
     * 类别主键(level=1)
     */
    public int CategoryId;
    /**
     * 类别名称(level=2)
     */
    public String CategoryName;
    public String Name;
    public String ShortName;
    public String Description;
    /**
     * 是否在售
     */
    public boolean IsSale;
    /**
     * 是否有分红
     */
    public int Fenhongxingzhi;

    public boolean IsShow;
    /**
     * 缩略图路径(网络路径)
     */
    public String Thumbnail;
    /**
     * 是否已离线保存
     */
    public boolean IsLocal;
    /**
     * 保障期间 多个用逗号(,)分割
     */
    public String GuaranteePeriod;
    /**
     * 缴费期间 多个用逗号(,)分割
     */
    public String PaymentPeriod;
    /**
     * 最小投保年龄
     */
    public String MinAge;
    /**
     * 最大投保年龄
     */
    public String MaxAge;

    /**
     * 销售日期
     */
    public Date StartSellingDate;

    /**
     * 停售日期
     */
    public Date StopSellingDate;
    /**
     * 销售渠道
     */
    public String SaleChannelIDs;

    /**
     * 数据版本号
     */
    public int ProductVersion;
    /**
     * 本地数据版本号
     */
    public int LocalProductVersion;
    public boolean LocalProductSuccess;
    public Date LocalProductUpdateTime;
    /**
     * 资源版本号
     */
    public int ResourceVersion;
    /**
     * 本地资源版本号
     */
    public int LocalResourceVersion;
    public boolean LocalResourceSuccess;
    public Date LocalResourceUpdateTime;
    /**
     * 产品在售标志
     */
    public int IsProposaled = 0;
    /**
     * 组合属性
     */
    public int CompositionProperty;
    public int AccSex;
    /**
     * 合同属性-0附加险-1主险
     */
    public int ContractProperty;
    /**
     * 职业类别
     */
    public int AccOccupation;

    public String CreateTime;
    /**
     * 计划书
     */
    public String InsurerProposalSysID;
    /**
     * 扩展json
     */
    public String ExtendJSON;
    /**
     * 排序
     */
    public String ListOrder;
    /**
     * 更新标志位
     */
    public int UpdateFlag;

    /**
     * 收藏此产品的客户Id
     */
    public int customerId;

    public int IsFav;
    public Date favCreateTime;
    public String companyName;
    public String companyShortName;
    /**
     * 投保选项json
     */
    public String insProductOptionsJson;
    public String ModelMap;
    /*产品排序-后台用*/
    public int productWeight;
    /*产品排序(用于十大品牌)*/
    public int Weight;
    /*备案日期*/
    public String RecordTime;

    // 是否已经选中
    public boolean isSelected;
    // 是否已经存在（之前选择过）
    public boolean isExists;
    // ShortName首字母
    public String firstLetter;

    //是否加入了对比池
    public int isAddCompare;
    public List<ResponseProductTag.TagsBean> tagsBeens;

    public ApiProduct.ModelMapBean modelMapBean;

    public String getRateType() {
        return rateType;
    }

    public void setRateType(String rateType) {
        this.rateType = rateType;
    }

    public String rateType;
    /**
     * @param products
     * @param category //int categoryId
     * @return
     */
    public static List<ProductInfo> getProducts(List<ProductInfo> products, ProductCategoryInfo category) {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        if (products == null)
            return list;
        for (ProductInfo product : products) {
            if (product.CategoryId == 1) {
                if (product.CategoryName.equalsIgnoreCase(category.getName())) {
                    list.add(product);
                }
            } else {
                if (product.CategoryId == category.getId()) {
                    list.add(product);
                }
            }
        }
        return list;
    }

    /**
     * 字段值是否满足条件
     *
     * @param fieldValue 字段值
     * @param values     用|分割
     * @return values为 null或者空时返回true
     */
    public boolean isSatisfy(String fieldValue, String values) {
        if (values == null)
            return true;
        if (values.length() == 0)
            return true;
        String[] arr1 = values.split("\\|");
        String[] arr2 = fieldValue.split(",");
        return ArrayUtil.hasIntersect(arr1, arr2);
    }

    /**
     * 产品数据有更新
     *
     * @return
     */
    public boolean localProductHasNewVersion() {
        return LocalProductVersion != ProductVersion;
    }

    /**
     * 产品资源有更新
     *
     * @return
     */
    public boolean localResourceHasNewVersion() {
        return LocalResourceVersion != ResourceVersion;
    }

    /**
     * 产品有更新
     *
     * @return
     */
    public boolean hasNewVersion() {
        return localProductHasNewVersion() || localResourceHasNewVersion();
    }

    /**
     * 有版本号，即可以点击查看
     *
     * @return
     */
    public boolean hasVersion() {
        return ProductVersion > 0 && ResourceVersion > 0;
    }

    /**
     * 更新本地产品后调用 用于标记该产品已是最新
     */
    public void setNoNewVersion() {
        LocalProductVersion = ProductVersion;
        LocalResourceVersion = ResourceVersion;
        IsLocal = true;
    }

    // FIXME Blithe20140429

    /**
     * 更新本地产品后调用 用于标记该产品已是最新
     */
    public void setNoNewVersion(int productVersion, int resVersion) {
        LocalProductVersion = productVersion;
        LocalResourceVersion = resVersion;
        ProductVersion = productVersion;
        ResourceVersion = resVersion;
        IsLocal = true;
    }

    private CompanyInfo companyInfo;

    public void setCompanyInfo(CompanyInfo company) {
        companyInfo = company;
    }

    public CompanyInfo getCompanyInfo() {
        return companyInfo;
    }

    public ProductDatainfo getInfo() {
        ProductDatainfo info = new ProductDatainfo();
        info.ProductId = Id;
        info.CompanyId = CompanyId;
        info.LocalProductVersion = LocalProductVersion;
        info.ProductVersion = ProductVersion;
        info.LocalResourceVersion = LocalResourceVersion;
        info.ResourceVersion = ResourceVersion;
        info.IsLocal = IsLocal;
        info.Thumbnail = Thumbnail;
        if (companyInfo != null) {
            info.compInfo = companyInfo.getInfo();
        }

        return info;
    }

    /***
     * 1、首先比较 是否可下载（ResourceVersion>0） 2、然后比较是否在售（IsProposaled）。
     * 3、最后比较排列顺序（ListOrder）
     */
    @Override
    public int compareTo(ProductInfo another) {
        if (another.ResourceVersion > 0 && this.ResourceVersion > 0) {// 是否在本地
            // if(another.IsProposaled==this.IsProposaled){
            if (this.ListOrder != null && another.ListOrder != null) {
                return this.ListOrder.compareTo(another.ListOrder);
            } else {
                if (this.ListOrder == null && another.ListOrder != null) {
                    return 1;
                } else if (this.ListOrder != null && another.ListOrder == null) {
                    return -1;
                } else {
                    return 0;
                }
            }
        } else {
            if (another.ResourceVersion > 0) {
                return 1;
            } else {
                return -1;
            }
        }

    }

    /**
     * 合同属性字符串
     *
     * @return
     */
    public String getContractPropertyStr(int ContractProperty) {
        String result = "";
        switch (ContractProperty) {
            case 0:
                result = "附加险";
                break;
            case 1:
                result = "主险";
                break;
            case 2:
                result = "独立险种";
                break;
        }
        return result;
    }

    /**
     * 组合属性
     *
     * @param CompositionProperty
     * @return
     */
    public String getCompositionPropertyStr(int CompositionProperty) {
        String result = "";
        switch (CompositionProperty) {
            case 1:
                result = "单一险";
                break;
            case 2:
                result = "组合险";
                break;
        }
        return result;
    }

    /**
     * 投保性别
     *
     * @param AccSex
     * @return
     */
    public String getAccSexStr(int AccSex) {
        String result = "";
        switch (AccSex) {
            case 0:
                result = "女";
                break;
            case 1:
                result = "男";
                break;
            default:
                result = "不区分";
                break;
        }
        return result;
    }

    /**
     * 投保性别
     *
     * @param AccSex
     * @return
     */
    public String getAccSexStr2(int AccSex) {
        String result = "";
        switch (AccSex) {
            case 0:
                result = "无限制";
                break;
            case 1:
                result = "仅女性可";
                break;
            default:
                result = "仅男性可";
                break;
        }
        return result;
    }

    /**
     * 职业类别大写 "一", "二", "三", "四", "五", "六"
     *
     * @return
     */
    public String getCareerCategoryCn(int AccOccupation) {
        if (AccOccupation == 0 || AccOccupation > 6)
            return "数据异常";
        String[] num = new String[]{"零", "一", "二", "三", "四", "五", "六"};

        return num[AccOccupation];
    }

    /**
     * 获取Name的拼音首字母
     *
     * @return
     */
    public String getFirstLetter() {
        if (TextUtils.isEmpty(firstLetter)) {
            String pinyinString = PinyinUtil.generateDisplayStringMultiple(Name);
            if (!TextUtils.isEmpty(pinyinString)) {
                firstLetter = pinyinString.substring(0, 1);
            }
        }
        return firstLetter;
    }

    /**
     * 设置Name的拼音首字母字段FirstLetter
     */
    public void setFirstLetter() {
        if (TextUtils.isEmpty(firstLetter)) {
            String pinyinString = PinyinUtil.generateDisplayStringMultiple(Name);
            if (!TextUtils.isEmpty(pinyinString)) {
                firstLetter = pinyinString.substring(0, 1);
            }
        }
    }

}
